﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Windows.Forms;
using System.Diagnostics;

using TravModelSpace;

namespace TravWinControllerSpace
{
    public partial class TravWinController : Form
    {
        //
        ///// File menu
        //

        private void fileMenu_DropDownOpening (object sender, EventArgs e)
        {
            fileMenuOpenFolder.Enabled  = !Trav.IsBusy;
            fileMenuCloseFolder.Enabled = !Trav.IsBusy && Trav.Tree != null;
            fileMenuSelectPlaylist.Enabled = !Trav.IsBusy && details.Items.Count > 0;
            fileMenuSaveSelection.Enabled = !Trav.IsBusy && details.SelectedItems.Count > 0;
        }


        private void fileMenuOpenFolder_Click (object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog ();
            if (dialog.ShowDialog () == DialogResult.OK)
            {
                if (Trav.Tree != null)
                    Trav.CloseRoot ();

                Trav.OpenRootAsync (dialog.SelectedPath);
            }
        }


        private void fileMenuCloseFolder_Click (object sender, EventArgs e)
        {
            Trav.CloseRoot ();
        }


        private void fileMenuSaveSelection_Click (object sender, EventArgs e)
        {
            SaveFileDialog dialog = new SaveFileDialog ();
            dialog.InitialDirectory = Trav.CurrentDir;
            dialog.Filter = "playlists (*.m3u)|*.m3u|All files (*.*)|*.*";

            if (dialog.ShowDialog () == DialogResult.OK)
                Trav.SaveSelectionToPlaylist (dialog.FileName, false);
        }


        private void fileMenuSelectPlaylist_Click (object sender, EventArgs e)
        {
            OpenFileDialog dialog = new OpenFileDialog ();
            dialog.Filter = "playlists (*.m3u)|*.m3u|All files (*.*)|*.*";
            if (dialog.ShowDialog () == DialogResult.OK)
                Trav.SelectFromPlaylist (dialog.FileName);
        }


        private void fileMenuAbout_Click (object sender, EventArgs e)
        {
            MessageBox.Show (this,
                TravWinAbout.Product + ": " + TravWinAbout.Description + "\r" + TravWinAbout.Copyright + "\r",
                "About Traveler v" + TravWinAbout.Version,
                MessageBoxButtons.OK, MessageBoxIcon.Asterisk, MessageBoxDefaultButton.Button1);
        }


        private void fileMenuExit_Click (object sender, EventArgs e)
        {
            Application.Exit ();
        }

        //
        ///// Edit menu
        //

        private void editMenu_DropDownOpening (object sender, EventArgs e)
        {
            if (Trav.IsBusy)
            {
                foreach (ToolStripItem item in editMenu.DropDownItems)
                    if (item is ToolStripMenuItem)
                        item.Enabled = false;
                return;
            }

            editMenuCopy.Enabled = Trav.Detail.Presenter.SelectedCount > 0;
            editMenuDelete.Enabled = Trav.Detail.Presenter.SelectedCount > 0;
            editMenuSelectAll.Enabled = Trav.Detail.Presenter.Count > 0;
            editMenuInvert.Enabled = Trav.Detail.Presenter.SelectedCount > 0;
            editMenuSelectDuplicates.Enabled = Trav.Detail.Presenter.Count >= 2;
            editMenuNext.Enabled = Trav.Detail.Presenter.SelectedCount > 0; 

            if (!Trav.HasCurrentDir)
                editMenuPaste.Enabled = false;
            else
            {
                object fd = Clipboard.GetData (DataFormats.FileDrop);
                if (fd != null)
                {
                    string[] sx = (string[]) fd;
                    if (sx.Length > 0)
                        editMenuPaste.Enabled = true;
                }
            }
        }


        private void editMenuCopy_Click (object sender, EventArgs e)
        {
            string[] files = new string[Trav.Detail.Presenter.SelectedCount];
            int index = 0;

            foreach (string fullName in Trav.Detail.Presenter.SelectedFullNames)
                files[index++] = fullName;

            DataObject j = new DataObject (DataFormats.FileDrop, files);
            Clipboard.SetDataObject (j);
        }


        private void editMenuPaste_Click (object sender, EventArgs e)
        {
            object dropped = Clipboard.GetData (DataFormats.FileDrop);
            if (dropped != null)
            {
                string[] sourceFiles = (string[]) dropped;
                Trav.CopyFiles (sourceFiles, Trav.CurrentDir);
            }
        }


        private void editMenuDelete_Click (object sender, EventArgs e)
        {
            Trav.DeleteSelectedFiles ();
        }


        private void editMenuSelectAll_Click (object sender, EventArgs e)
        {
            Trav.SelectAll ();
        }


        private void editMenuInvert_Click (object sender, EventArgs e)
        {
            Trav.InvertSelection ();
        }


        private void editMenuSelectDuplicates_Click (object sender, EventArgs e)
        {
            Trav.SelectDuplicates ();
        }


        private void editMenuNext_Click (object sender, EventArgs e)
        {
            int lastViewedIndex = 0;

            for (; lastViewedIndex < details.Items.Count; ++lastViewedIndex)
            {
                Point p = details.Items[lastViewedIndex].Position;
                if (p.Y > details.Size.Height)
                    break;
            }

            int newItemIndex = -1;

            for (int i1 = 0; i1 < details.SelectedItems.Count; ++i1)
            {
                if (details.SelectedItems[i1].Index > lastViewedIndex)
                {
                    newItemIndex = details.SelectedItems[i1].Index;
                    details.Items[newItemIndex].EnsureVisible ();
                    break;
                }
            }
        }

        //
        ///// Folder menu
        //

        private void folderMenu_DropDownOpened (object sender, EventArgs e)
        {
            folderMenuExpandAll.Enabled = !Trav.IsBusy && Trav.Tree != null;
            folderMenuNew.Enabled = !Trav.IsBusy && Trav.HasCurrentDir;
        }


        private void folderMenuExpandAll_Click (object sender, EventArgs e)
        {
            folders.ExpandAll ();
        }


        private void folderMenuNew_Click (object sender, EventArgs e)
        {
            TreeNode node = folders.SelectedNode;

            if (node != null)
            {
                TreeNode newNode = node.Nodes.Add ("");
                newNode.EnsureVisible ();
                folders.LabelEdit = true;
                newNode.BeginEdit ();
            }
        }

        //
        ///// Detail main menu
        //

        private void detailMenu_DropDownOpened (object sender, EventArgs e)
        {
            detailMenuShowSubfolders.Checked = Trav.Detail.IsSubfoldersShown;
        }


        private void detailMenuArrangeBySelected_Click (object sender, EventArgs e)
        {
            sortDetailsColumn (-1, null);
        }


        private void detailMenuArrangeByName_Click (object sender, EventArgs e)
        {
            sortDetailsColumn (ColumnBaseName, "S");
        }

        private void detailMenuArrangeByType_Click (object sender, EventArgs e)
        {
            sortDetailsColumn (-2, null);
        }


        private void detailMenuShowSubfolders_Click (object sender, EventArgs e)
        {
            Trav.ShowSubfolders (!Trav.Detail.IsSubfoldersShown);
        }

        //
        ///// Detail context menu
        // 

        private void detailContext_Opening (object sender, CancelEventArgs e)
        {
            bool enable = !Trav.IsBusy && Trav.Detail.Presenter.SelectedCount > 0;
            detailContextOpen.Enabled = enable;
            detailContextSelectFrom.Enabled = enable;

        }

        private void detailContextOpen_Click (object sender, EventArgs e)
        {
            Trav.LaunchSelections ();
        }

        private void detailContextSelectFrom_Click (object sender, EventArgs e)
        {
            var selectionFullName = new List<string> ();

            foreach (string f in Trav.Detail.Presenter.SelectedFullNames)
                selectionFullName.Add (f);

            Trav.Detail.Presenter.SelectNone ();

            foreach (string f in selectionFullName)
                Trav.SelectFromPlaylist (f);
        }
    }
}
